package com.itheima.Service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.Mapper.CategoryMapper;
import com.itheima.Pojo.Category;
import com.itheima.Pojo.Dish;
import com.itheima.Pojo.Setmeal;
import com.itheima.Service.CategoryService;
import com.itheima.Service.DishService;
import com.itheima.common.CustomException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 在删除菜品时会出现菜品和套餐里包含菜，所以要先进行判断，是否包含菜品
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category>  implements CategoryService {

    @Autowired
    private DishService dishService;

    public void remove(long ids){
        LambdaQueryWrapper<Dish> dishLambdaQueryWrapper=new LambdaQueryWrapper<>();
        //添加查询条件，根据id进行查询
        dishLambdaQueryWrapper.eq(Dish::getCategoryId,ids);
        int count = (int) dishService.count(dishLambdaQueryWrapper);
        //判断菜品分类是否有菜品
        if(count>0){
            throw new CustomException("当前分类下关联了菜品，不能删除");
        }
        //判断套餐分类里面是否有菜品
        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper=new LambdaQueryWrapper<>();
        //添加查询条件，根据id进行查询
        dishLambdaQueryWrapper.eq(Dish::getCategoryId,ids);
        int count2 = (int) dishService.count(dishLambdaQueryWrapper);
        if(count2>0){
            throw new CustomException("当前分类下关联了套餐，不能删除");
        }

        //都不包含 ，显示 删除成功
        super.removeById(ids);
    }
}
